// ============================================================================
// TaskLog 实体定义
// ============================================================================

import {
  Entity,
  PrimaryColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  JoinColumn,
  Index,
  BeforeInsert
} from 'typeorm'
import { Task } from './task.entity'
import { JsonTransformer } from '../transformers/json.transformer'
import { LogLevel, TaskLogEntry } from '../../../shared/types'
import { v4 as uuidv4 } from 'uuid'

/**
 * 任务日志实体
 */
@Entity('task_logs')
@Index(['taskId'])
@Index(['level'])
@Index(['createdAt'])
export class TaskLog {
  @PrimaryColumn({ type: 'varchar' })
  id: string

  @Column({ name: 'task_id', type: 'varchar' })
  taskId: string

  @Column('varchar')
  level: string

  @Column({ type: 'text' })
  message: string

  @Column({ 
    type: 'text', 
    nullable: true, 
    transformer: new JsonTransformer() 
  })
  data?: any

  @Column({ nullable: true, type: 'varchar' })
  source?: string

  @CreateDateColumn({ name: 'created_at' })
  createdAt: Date

  @UpdateDateColumn({ name: 'updated_at' })
  updatedAt: Date

  // 关系
  @ManyToOne(() => Task, task => task.logs, { 
    onDelete: 'CASCADE',
    eager: false 
  })
  @JoinColumn({ name: 'task_id' })
  task: Task

  @BeforeInsert()
  generateId() {
    if (!this.id) {
      this.id = uuidv4()
    }
  }

  /**
   * 创建信息日志
   */
  static info(taskId: string, message: string, data?: any, source?: string): TaskLog {
    const log = new TaskLog()
    log.taskId = taskId
    log.level = 'info'
    log.message = message
    log.data = data
    log.source = source
    return log
  }

  /**
   * 创建警告日志
   */
  static warn(taskId: string, message: string, data?: any, source?: string): TaskLog {
    const log = new TaskLog()
    log.taskId = taskId
    log.level = 'warn'
    log.message = message
    log.data = data
    log.source = source
    return log
  }

  /**
   * 创建错误日志
   */
  static error(taskId: string, message: string, data?: any, source?: string): TaskLog {
    const log = new TaskLog()
    log.taskId = taskId
    log.level = 'error'
    log.message = message
    log.data = data
    log.source = source
    return log
  }

  /**
   * 创建调试日志
   */
  static debug(taskId: string, message: string, data?: any, source?: string): TaskLog {
    const log = new TaskLog()
    log.taskId = taskId
    log.level = 'debug'
    log.message = message
    log.data = data
    log.source = source
    return log
  }

  /**
   * 转换为 TaskLogEntry 格式（向后兼容）
   */
  toTaskLogEntry(): TaskLogEntry {
    return {
      id: this.id,
      level: this.level as LogLevel,
      message: this.message,
      data: this.data,
      source: this.source,
      timestamp: this.createdAt
    }
  }

  /**
   * 从 TaskLogEntry 创建日志实体
   */
  static fromTaskLogEntry(taskId: string, logEntry: any): TaskLog {
    const log = new TaskLog()
    log.id = logEntry.id || uuidv4()
    log.taskId = taskId
    log.level = logEntry.level
    log.message = logEntry.message
    log.data = logEntry.data
    log.source = logEntry.source
    if (logEntry.timestamp) {
      log.createdAt = new Date(logEntry.timestamp)
    }
    return log
  }
}
